import com.stein.utils.EsClient;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ESMap {

    private RestHighLevelClient client = EsClient.getClient();
    //索引库名字
    private String index = "map";


    /**
     * 多边形查询
     * @throws Exception
     */
    @Test
    public void duoTest() throws  Exception{
        SearchRequest request = new SearchRequest();

        SearchSourceBuilder builder = new SearchSourceBuilder();
        List<GeoPoint> points = new ArrayList<>();
        points.add(new GeoPoint(40.120736,116.255559));
        points.add(new GeoPoint(40.120901,116.263069));
        points.add(new GeoPoint(40.127089,116.258748));
        points.add(new GeoPoint(40.126999,116.251471));

        builder.query(QueryBuilders.geoPolygonQuery("location",points));

        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        for (SearchHit hit : response.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }

    }

    /**
     *对角线查询
     *
     * @throws Exception
     */
    @Test
    public void duiTest() throws  Exception{
        SearchRequest request = new SearchRequest();

        SearchSourceBuilder builder = new SearchSourceBuilder();
//        List<GeoPoint> points = new ArrayList<>();
//        points.add(new GeoPoint(40.120736,116.255559));
//        points.add(new GeoPoint(40.120901,116.263069));
//        points.add(new GeoPoint(40.127089,116.258748));
//        points.add(new GeoPoint(40.126999,116.251471));

        builder.query(QueryBuilders.geoBoundingBoxQuery("location").setCorners(new GeoPoint(40.136596,116.227716),new GeoPoint(40.100236,116.308491)));

        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        for (SearchHit hit : response.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }

    }

    /**
     *圆圈查询
     *
     * @throws Exception
     */
    @Test
    public void circleTest() throws  Exception{
        SearchRequest request = new SearchRequest();

        SearchSourceBuilder builder = new SearchSourceBuilder();
//        List<GeoPoint> points = new ArrayList<>();
//        points.add(new GeoPoint(40.120736,116.255559));

//        points.add(new GeoPoint(40.120901,116.263069));
//        points.add(new GeoPoint(40.127089,116.258748));
//        points.add(new GeoPoint(40.126999,116.251471));

        builder.query(QueryBuilders.geoDistanceQuery("location").point(new GeoPoint(40.120736,116.255559)).distance("500km"));

        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        for (SearchHit hit : response.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }

    }
}
